package com.example.bailin.giftsay.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.GuanZhuEntity;
import com.example.bailin.giftsay.tool.DBTool;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.UrlProvide;

import java.util.ArrayList;

/**
 * Created by bailin on 16/8/24.
 */
public class DanPFragment extends BaseFragment {

    private RecyclerView rv;
    private MeDpAdapter adapter;
    private SharedPreferences shared;
    private GridLayoutManager manager;

    @Override
    protected int setLayout() {
        return R.layout.fragment_danp;
    }


    @Override
    protected void initView() {
        rv = bindView(R.id.rv_me_dp);
    }

    @Override
    protected void initData() {
        shared = getActivity().getSharedPreferences(UrlProvide.NAME, Context.MODE_PRIVATE);
        manager = new GridLayoutManager(MyApp.getContext(), 2);
        adapter = new MeDpAdapter();
        show();
    }

    @Override
    public void onResume() {
        super.onResume();
        show();
    }

    private void show() {
        String userName = shared.getString("username", "未登录");
        if (!userName.equals("未登录")) {
            DBTool.getInstance().queryUserName("userName = ?", userName, new DBTool.QueryListener() {
                @Override
                public void onQueryComplete(ArrayList<GuanZhuEntity> guanZhuEntities) {
                    adapter.setGuanZhuEntities(guanZhuEntities);
                    adapter.setIndex(guanZhuEntities.size());
                }
            });
            rv.setLayoutManager(manager);
            rv.setAdapter(adapter);
        } else {
            adapter.setIndex(0);
        }
    }
}
